#include<Windows.h>
#include"usbreceive.h"

Receiver::Receiver(void)
{
  m_pUsbDevice=NULL;
  m_bOpen=false;
  m_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);//BULK

}

Receiver::~Receiver(void)
{
    CloseHandle(m_hEvent);
    if (m_pUsbDevice)
    {
        if (m_pUsbDevice->IsOpen())
        {
            m_pUsbDevice->Reset();
            m_pUsbDevice->Close();
        }
        delete m_pUsbDevice;
    }
}

bool Receiver::IsDeviceOpen()
{
  m_bOpen=m_pUsbDevice->IsOpen();
  return m_bOpen;
}

bool Receiver::DetectHardDevice(void)
{
    if(IsDeviceOpen())
    {
        if ((m_pUsbDevice->VendorID==VID_OF_newHF) && (m_pUsbDevice->ProductID==PID_OF_newHF))
           {
                m_hardType=NEWHF;

           }
        else
           {

                m_hardType=HF2013;
           }

         return true;
    }
    return false;
}

bool Receiver::usbOpen()
{
    if(m_pUsbDevice!=NULL)
    {
        delete m_pUsbDevice;
    }
    m_pUsbDevice =new CCyUSBDevice((HANDLE)0, CYUSBDRV_NEW_GUID, true);
    DetectHardDevice();
    unsigned char DeNum=m_pUsbDevice->DeviceCount();
    if(DeNum==1)
    {
        //只连接有一个设备，直接打开设备
                if (!m_pUsbDevice->IsOpen())
                {
                    m_bOpen=m_pUsbDevice->Open(0);
                }
    }
    else
    {

        m_bOpen=false;
    }
    return m_bOpen;
}

// 清空FIFO
bool Receiver::ResetFIFO(void)
{
    return WriteToCtrlEP(0,0,REQ_OF_RESET_FIFO,TGT_DEVICE,REQ_VENDOR,DIR_TO_DEVICE);
}

//ws:控制端口直接控制命令写入
bool Receiver::WriteToCtrlEP(unsigned short nValue,unsigned short nIndex,unsigned short reqCode,
    CTL_XFER_TGT_TYPE targetType,CTL_XFER_REQ_TYPE reqType,CTL_XFER_DIR_TYPE dirType)
{	bool bSuccess=false;
    if (IsDeviceOpen())
    {
        CCyControlEndPoint* CtrlEP=m_pUsbDevice->ControlEndPt;
        if (CtrlEP==0)
        {
            return false;
        }
        long nBufferSize=0;
        unsigned char* pBuffer=new unsigned char[nBufferSize];
        //通信对象，设备
        CtrlEP->Target=targetType;
        //请求类型
        CtrlEP->ReqType=reqType;
        //传输方向：由上位机到设备
        CtrlEP->Direction=dirType;
        //请求码：0xDA:控制命令
        CtrlEP->ReqCode=reqCode;
        //序号
        CtrlEP->Index=nIndex;

        switch(reqCode)
        {
        case REQ_OF_SET_FPGA_CTRL_SELFPLUSE:
            {
                unsigned char chValue=(nValue&0xFF);
                unsigned char chAddr=nValue>>8;
                nValue=VALUE(chAddr*4,chValue);
                for (int i=0;i<3;i++)
                {
                    if (i==1)
                    {
                        CtrlEP->Value=nValue|0x0200;
                    }
                    else
                    {
                        CtrlEP->Value=nValue&0xFCFF;
                    }
                    bSuccess=CtrlEP->Write(pBuffer,nBufferSize);
                }
                break;
            }
        default:
            {
                //请求值
                CtrlEP->Value=nValue;
                bSuccess=CtrlEP->Write(pBuffer,nBufferSize);
                break;
            }
        }
        delete[] pBuffer;
    }
    return bSuccess;
}
